home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / OK, What was that? / Extension / OK.c next >
Encoding:
C/C++ Source or Header  |  1992-07-03  |  4.9 KB  |  241 lines  |  [TEXT/MPS ]

  1. #include <Dialogs.h>
  2. #include <GestaltEqu.h>
  3. #ifndef        __TYPES__
  4. #include    <Types.h>
  5. #endif
  6.  
  7. #ifndef        __MEMORY__
  8. #include    <Memory.h>
  9. #endif
  10.  
  11. #ifndef        __ERRORS__
  12. #include    <Errors.h>
  13. #endif
  14.  
  15. #ifndef        __TRAPS__
  16. #include    <Traps.h>
  17. #endif
  18.  
  19. #ifndef        __OSUTILS__
  20. #include    <OSUtils.h>
  21. #endif
  22.  
  23. #ifndef        __TOOLUTILS__
  24. #include    <ToolUtils.h>
  25. #endif
  26.  
  27. #include <Resources.h>
  28. #include "OSUtilsSup.h"
  29.     
  30. pascal void ShowINIT(short iconID, short moveX);
  31. pascal void MessageModalDialog(ModalFilterProcPtr filterProc,short *itemHit);
  32. pascal OSErr MessageGestalt(OSType selector, long *response);
  33. pascal Boolean MessageFilterProc(DialogPtr theDialog, EventRecord* theEvent, short* itemHit);
  34. void foo(void);
  35. void globals(void);
  36.  
  37. void main(void)
  38. {
  39.     
  40.     QHdrPtr q = (QHdrPtr)foo;
  41.     
  42.     q->qFlags = 0;
  43.     q->qHead = 0;
  44.     q->qTail = 0;
  45.     
  46.     NewGestalt('Msg!',(ProcPtr)MessageGestalt);
  47.     
  48.     (void)PatchTrap((ProcPtr*)globals, _ModalDialog, (ProcPtr)MessageModalDialog, 0);
  49.     DetachResource(RecoverHandle((Ptr)main));
  50.     
  51.     ShowINIT(128,-1);
  52.  
  53. }
  54.  
  55. void globals(void)
  56. {
  57.     Debugger();
  58.     Debugger();
  59.     Debugger();
  60.     Debugger();
  61.     Debugger();
  62.     Debugger();
  63.     Debugger();
  64.     Debugger();
  65. }
  66.  
  67. void foo(void)
  68. {
  69.     Debugger();
  70.     Debugger();
  71.     Debugger();
  72.     Debugger();
  73.     Debugger();
  74.     Debugger();
  75.     Debugger();
  76.     Debugger();
  77. }
  78.  
  79. pascal OSErr MessageGestalt(OSType, long *response)
  80. {
  81.     *response = (long)foo;
  82.     return noErr;
  83. }
  84.  
  85. typedef struct {
  86.     char         blank[6];
  87.     PicHandle    picture;
  88. } Q;
  89.  
  90. typedef pascal void (*SModalDialog)(ModalFilterProcPtr, short*);
  91.  
  92. typedef struct {
  93.     long                refCon;
  94.     long                ticks;
  95.     ModalFilterProcPtr    filterProc;
  96.     short                stage;
  97.     Boolean                timedOut;
  98. } Locals;
  99.  
  100. pascal void MessageModalDialog(ModalFilterProcPtr filterProc,short *itemHit)
  101. {
  102.     PicHandle     picture;
  103.     GrafPtr        theDialog;
  104.     THz            zone;
  105.     Q*            q;
  106.     GrafPtr        oldPort;
  107.     QHdrPtr        qHeader;
  108.     short        itemType;
  109.     Handle        handle;
  110.     Rect        box;
  111.     Str255        title;
  112.     Locals        locals;
  113.     
  114.     theDialog = FrontWindow();
  115.     
  116.     GetDItem(theDialog, ok, &itemType, &handle, &box);
  117.     
  118.     if (itemType != 4) goto bannana;
  119.     GetCTitle((ControlHandle)handle, title);
  120.     if (title[0] != 2) goto bannana;
  121.     if (title[1] != 'O') goto bannana;
  122.     if (title[2] != 'K') goto bannana;
  123.     
  124.     locals.filterProc    = filterProc;
  125.     locals.refCon        = ((WindowPeek)theDialog)->refCon;
  126.     locals.ticks        = TickCount();
  127.     locals.timedOut        = false;
  128.     locals.stage        = 0;
  129.     
  130.     ((WindowPeek)theDialog)->refCon = &locals;
  131.  
  132. #define CallTheOldTrap    (*((SModalDialog*)globals))
  133.     
  134.     CallTheOldTrap(MessageFilterProc, itemHit);
  135.     
  136.     ((WindowPeek)theDialog)->refCon = locals.refCon;
  137.     
  138.     if (!locals.timedOut) return;
  139.     
  140.     GetPort(&oldPort);
  141.     SetPort(theDialog);
  142.     
  143.     zone = GetZone();
  144.     SetZone(SystemZone());
  145.     picture = OpenPicture(&theDialog->portRect);
  146.     SetZone(zone);
  147.     
  148.     (*(ControlHandle)handle)->contrlHilite = inButton;
  149.     DrawDialog(theDialog);
  150.     (*(ControlHandle)handle)->contrlHilite = 0;
  151.     
  152.     ClosePicture();
  153.     
  154.     SetPort(oldPort);
  155.  
  156.     q = (Q*)NewPtrSys(6 + sizeof(PicHandle));
  157.     q->picture = picture;
  158.     
  159.     if (Gestalt('Msg!', (long*)&qHeader) == noErr) Enqueue((QElemPtr)q, qHeader);
  160.     else {
  161.         DisposePtr((Ptr)q);
  162.         KillPicture(picture);
  163.     }
  164.     SetPort(oldPort);
  165.     
  166.     *itemHit = ok;
  167.     return;
  168. bannana:
  169.     CallTheOldTrap(filterProc, itemHit);
  170. }
  171.  
  172. enum {
  173.     lastStage        = 8,                        /* Dismiss Dialog when this stage is met */
  174.     middleStage        = 3,                        /* Invert button after middleStage */
  175.     startTime        = 60 * 10,                    /* Start to fade after 15 seconds */
  176.     delayTime        = (60 * 3) / lastStage        /* Fade in 3 seconds */
  177. };
  178.  
  179. pascal Boolean MessageFilterProc(DialogPtr theDialog, EventRecord* theEvent, short* itemHit)
  180. {
  181.     Locals* locals = ((WindowPeek)theDialog)->refCon;
  182.     short    itemType;
  183.     Handle    handle;
  184.     Rect    box;
  185.     GrafPtr    currentPort;
  186.     short    patternIndex[] = { 21, 22, 23, 4, 23, 22, 21, 20 };
  187.     Pattern    pattern;
  188.     
  189.     if ((theEvent->what >= mouseDown) && (theEvent->what <= autoKey)) {
  190.         
  191.         /* Brighten the control */
  192.         
  193.         if (locals->stage != 0) {
  194.             GetDItem(theDialog, ok, &itemType, &handle, &box);
  195.             Draw1Control((ControlHandle)handle);
  196.             locals->stage = 0;
  197.         }
  198.     
  199.         /* Reset the timer */
  200.         
  201.         locals->ticks = TickCount();
  202.         
  203.     } else if (locals->ticks + startTime + (locals->stage) * delayTime < TickCount()) {
  204.         if (locals->stage == lastStage) {
  205.             *itemHit = ok;
  206.             locals->timedOut = true;
  207.             
  208.             return true;
  209.         }
  210.         GetDItem(theDialog, ok, &itemType, &handle, &box);
  211.         GetPort(¤tPort);
  212.         SetPort(theDialog);
  213.         if (locals->stage > middleStage) {
  214.             (*(ControlHandle)handle)->contrlHilite = inButton;
  215.             Draw1Control((ControlHandle)handle);
  216.             (*(ControlHandle)handle)->contrlHilite = 0;
  217.             PenMode(patBic);
  218.         } else {
  219.             Draw1Control((ControlHandle)handle);
  220.             PenMode(patOr);
  221.         }
  222.         GetIndPattern(&pattern, sysPatListID, patternIndex[locals->stage]);
  223.         PenPat(&pattern);
  224.         PaintRoundRect(&box, 10, 10);
  225.         PenNormal();
  226.         SetPort(currentPort);
  227.         ++locals->stage;
  228.     }
  229.     
  230.     if (locals->filterProc) {
  231.         Boolean result;
  232.         
  233.         ((WindowPeek)theDialog)->refCon = locals->refCon;
  234.         result = locals->filterProc(theDialog, theEvent, itemHit);
  235.         ((WindowPeek)theDialog)->refCon = locals;
  236.         return result;
  237.     } else return false;
  238. }
  239.  
  240.  
  241.